package client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class Login extends JPanel implements ActionListener{
   static JTextField username;
    public Login(){
        JLabel lusername = new JLabel("Name :");
        username = new JTextField(20);
        JPanel g1 = new JPanel();
        g1.setLayout(new BoxLayout(g1,BoxLayout.X_AXIS));
        lusername.setPreferredSize(new Dimension(100, 25));
        g1.add(lusername);
        g1.add(username);



        JPanel info = new JPanel();
        info.setLayout(new BoxLayout(info,BoxLayout.Y_AXIS));
        info.add(g1);



        JButton b1 = new JButton("Login");
        JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        b1.setActionCommand("Login");
        b1.addActionListener(this);
        button.add(b1);

        setLayout(new BorderLayout(5,5));
        add(button,BorderLayout.PAGE_END);
        add(info,BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {
        String strActionCommand = e.getActionCommand();
        String user = username.getText();
        if(strActionCommand.equals("Login")){
            if(!user.equals("")){
               int check =  TCPClient.getConnect(user);
               switch(check){
                   case 3: JOptionPane.showMessageDialog(this, "Not connect");break;
                   default: {
                       if(TCPClient.checkFile("port.txt")==1){
                        JOptionPane.showMessageDialog(this, "Login Ok!");
                            Main.framelogin.dispose();
                            Main.createAndShowGUI();
                            Main.frame.pack();
                            Main.frame.setVisible(true);
                       }
                       else{
                           JOptionPane.showMessageDialog(this, "Login fail or can you can be !");
                       }
                   }break;
               }
            }
            else
                JOptionPane.showMessageDialog(this, "Fill name");
        }
    }
}